package com.example.authdemo;

import com.example.authdemo.slice.MainAbilitySlice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.biometrics.authentication.BiometricAuthentication;
import ohos.biometrics.authentication.IFaceAuthentication;

public class MainAbility extends Ability {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setMainRoute(MainAbilitySlice.class.getName());

        try {
            // 获取生物验证实例
            BiometricAuthentication biometricAuthentication = BiometricAuthentication.getInstance(this);
            // 检查是否有生物特征验证能力
            int canAuth = biometricAuthentication.checkAuthenticationAvailability(BiometricAuthentication.AuthType.AUTH_TYPE_BIOMETRIC_FACE_ONLY, BiometricAuthentication.SecureLevel.SECURE_LEVEL_S2, true);
            // 如果支持验证能力
            if (canAuth == BiometricAuthentication.BA_CHECK_SUPPORTED) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        // 执行验证动作
                        int code = biometricAuthentication.execAuthenticationAction(
                                BiometricAuthentication.AuthType.AUTH_TYPE_BIOMETRIC_FACE_ONLY,
                                BiometricAuthentication.SecureLevel.SECURE_LEVEL_S2, true, false, null);
                        if (code == IFaceAuthentication.FaceAuthResultCode.FACE_AUTH_RESULT_SUCESS) {
                            // 验证成功，做后续业务逻辑
                        }

                        // 获取提示信息
                        BiometricAuthentication.AuthenticationTips tips = biometricAuthentication.getAuthenticationTips();
                    }
                }).start();
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}
